package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.PictureDao;
import cn.sumpu.app.guoguojie.domain.Picture;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class PictureDaoImpl extends SqlSessionDaoSupport implements PictureDao {

	private final static Logger logger = LoggerFactory
			.getLogger(PictureDaoImpl.class);

	@Override
	public void insert(Picture picture) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.PictureMapper.insert",
					picture);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Insert database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Picture> getList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (List<Picture>) getSqlSession().selectList(
					"cn.sumpu.app.guoguojie.persistence.PictureMapper.getList",
					map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}
	
	

	@Override
	public int getListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.PictureMapper.getListCount",
					map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getListCount Pic failure.", e);
			}
			throw new AppBusinessException("getListCount Pic failure.", e);
		}
	}

	@Override
	public void update(Picture picture) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.PictureMapper.update",
					picture);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Update database failure.", e);
			}
			throw new AppBusinessException("Update database failure.", e);
		}
	}

	@Override
	public void deleteById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.PictureMapper.deleteById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Delete database failure.", e);
			}
			throw new AppBusinessException("Delete database failure.", e);
		}
	}

	@Override
	public Picture getById(String id) throws AppBusinessException {
		try {
			return (Picture) getSqlSession().selectOne(
					"cn.sumpu.app.guoguojie.persistence.PictureMapper.getById",
					id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Query database failure.", e);
			}
			throw new AppBusinessException("Query database failure.", e);
		}
	}

	@Override
	public void deleteByAlbum(String album) throws AppBusinessException {
		try {
			getSqlSession().delete(
					"cn.sumpu.app.guoguojie.persistence.PictureMapper.deleteByAlbum",album);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteByAlbum failure.", e);
			}
			throw new AppBusinessException("deleteByAlbum failure.", e);
		}
	}
	
	

}
